#ifndef T_MODULEGROUPRECTITEM_H
#define T_MODULEGROUPRECTITEM_H
#include <QObject>
#include <QGraphicsRectItem>
#include <QMenu>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
#include "common.h"

class T_ModuleGroupRectItem : public QObject, public QGraphicsRectItem
{
    Q_OBJECT

public:
    enum LayoutDirection { 
        Horizontal = 0,
        Vertical,
        DD  //堆叠
    };

    explicit T_ModuleGroupRectItem(QObject *parent = nullptr);

    void SetItemInfo(cutomitem data);
    void setSpacing(double space);
    void setLayoutDirection(LayoutDirection direction);
    void addRectangle(QGraphicsRectItem *rectItem);
    void removeRectangle(QGraphicsRectItem *rectItem);
    void updateLayout();    
    QString getItemName();
    cutomitem getItemData();
    qreal getSpacing();
    qreal getMars(LayoutDirectionHV direction);
    void setMars(qreal topMar,qreal leftMar,qreal bottomMar,qreal rightMar);
    int getDirection();

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    void paint(QPainter *painter,
              const QStyleOptionGraphicsItem *option,
              QWidget *widget) override;
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;

signals:
    void flushMainRect();
    void deleteItem();

private:
    T_ModuleGroupRectItem* createCopy() const;
    void setCursorShape();
    void setMarSpacing(qreal space_left,qreal space_top,qreal space_right,qreal space_bottom);
    void setNewOuterRectF(const QRectF &rectf);
    void setNewInnerRect(qreal width,qreal height);

    void calculateGroupSize();

    void deleteItems();

    void calaOldRects();
    void calcZoomRects(const QRectF &newRect);

    void drawGuideLines(QPainter *painter);
    void drawTipLines(QPainter *painter);

private:
    cutomitem m_item_data;

    QList<QGraphicsRectItem*> m_children;
    LayoutDirection m_direction = Horizontal;
    qreal m_spacing = 10.0;
    QPointF m_lastMousePos;
    QPointF m_lastPos;
    bool m_isDragging = false;

    ResizeDirection m_resizeDirection;

    // 添加以下成员变量
    QRectF m_originalContainerRect;
    QHash<QGraphicsRectItem*, QPointF> m_originalChildPositions;
    QHash<QGraphicsRectItem*, QRectF> m_originalChildRects;
    qreal m_originalSpacing = 0;
    bool m_isFirstResize = true;

    bool m_flag_realse = false; //鼠标释放的时候更新布局，不允许触发垂直水平交换

    //复制用的变量
    bool m_isCopying = false;
    // 复制体
    T_ModuleGroupRectItem* m_copyItem = nullptr;
    // 鼠标按下时的位置
    QPointF m_mousePressPos;
    // 是否显示辅助线
    bool m_showGuideLines = false;

    //边距变量
    qreal m_mar_spacing_left = 10.0;
    qreal m_mar_spacing_top = 10.0;
    qreal m_mar_spacing_right = 10.0;
    qreal m_mar_spacing_bottom = 10.0;

    //绘制辅助线
    bool m_is_tip{true};
};
#endif // T_MODULEGROUPRECTITEM_H
